package model.conversations;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import view.input.InputWords;


public class Conversation {
	int _maxReplies = 10;
	String greeting,
		   comeAgain,
		   youSaidThatAlready;
	// Making these public so they are easier to access
	// getters and setters can follow
	// So long as we are careful with access when we code
	// having things public shouldn't be harmful.
	public Map<Integer,Phrase> replies;
	public Map<Integer,Phrase> unavailableReplies;
	int currentPhrase;
	public String name;
	boolean successfulConversation=false;
	
	public Conversation(String _name,String _greeting){
		name=_name;
		replies=new HashMap<Integer,Phrase>();
		unavailableReplies = new HashMap<Integer,Phrase>(100);
		greeting = _greeting;
		comeAgain = "I don't understand you";
		youSaidThatAlready = "you already asked me that";
		currentPhrase = 0;
		// Using MAX_VALUE, since there will never be that many replies
		// DO NOT use 0, that is the index of the first reply
		// a better alternative is a huge value or -1
		//this.setReply(-1,"No. I need to best men for this mission:not this time","I:come|join:journey|expedition|mission|Stonehaven",false,false);
		//this.setReply(-1,"Come where?","I:must:come",false,false);
		//this.setReply(1,"How?","I:useful",false,false);
		//this.setReply(1,"Sounds like we could need you:ok I'm sold","I:cook|spy|scout",true,true);
	}

	// I can't say I understand this unavailable replies stuff
	public void setReply(int prerequisiteId,String hint,String iSay, String uSay,boolean finalReply,boolean successfulReply){
		//Add phrase to reply list
		// There is no need to call our own function to get our array, we own it!
		int size = replies.size()+unavailableReplies.size()+1;
		//System.out.println(":"+size);
		Phrase p = new Phrase(size,prerequisiteId,hint,iSay,uSay,finalReply,successfulReply);
		
		//if there are no prerequisite
		if (prerequisiteId==0){
			replies.put(size,p);
		}
		else{
			// This falls over if you assign to an index before previous
			// ones have been assigned
			unavailableReplies.put(size,p);
		}
	}
	
	public String getReply(InputWords words){
		boolean success;		
		/*if(words.getWord1().equals("HINT"))
			return replies.get(replies.size()).getHint();*/
		for (Phrase phrase : replies.values()){
			//System.out.println("--------"+phrase.tellMeSomething());
			success=phrase.match(words);
			if (success){
				if(phrase.getSuccessfulPhraseStatus())
					successfulConversation=true;
				
				currentPhrase=phrase.getID();
				//System.out.println("success");
				if (phrase.getAnsweredStatus()){
					return youSaidThatAlready;
				}
				else{
					phrase.setAnsweredStatus(true);
					for (Phrase hidden : unavailableReplies.values()){
						if(hidden.getPrerequesite()==phrase.getID());
							replies.put(hidden.getID(),hidden);
							unavailableReplies.remove(String.valueOf(hidden.getID()));
						}
					}
					return phrase.tellMeSomething();
				}
			}
		return comeAgain;
	}
	
	public String getGreeting()
	{
		return this.greeting;
	}
	
	
	public boolean getStatusFinalReply(){
		return successfulConversation;
		
	}

	public void setReplies(List<Phrase> replies) {
		/* Some sort of conversion will have to occur
		 * this function to work.
		 */
		//this.replies = replies;
		System.out.println("You tried to set replies.\nI didn't do it.\nSorry");
	}
	/* This functions works, although I haven't checked for dependencies */
	public void setReplies(Map<Integer,Phrase> replies) {
		this.replies = replies;
	}
}
